package com.lge.qcontact.configuration;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.lge.qcontact.R;
import com.lge.qcontact.utils.LocalDBHelper;

public class SetAutoLoginActivity extends Activity {
	private EditText idField;
	private EditText pwField;
	private CheckBox autoLoginChk;
	private SQLiteDatabase localDB;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_set_autologin);
		
		idField = (EditText) findViewById(R.id.autoLoginIDField);
		pwField = (EditText) findViewById(R.id.autoLoginPWField);
		
		autoLoginChk = (CheckBox) findViewById(R.id.autoLoginChk);
		autoLoginChk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					localDB.execSQL("update LOGIN_INFO " +
							   				  "set isAutoLogin='true'");
				}else{
					localDB.execSQL("update LOGIN_INFO " +
			   				   				  "set isAutoLogin='false'");
				}
			}
		});
		
		
		
		localDB = (new LocalDBHelper(this)).getReadableDatabase();
		
		Cursor c = localDB.rawQuery("select * from LOGIN_INFO;", null);
		
		c.moveToNext();
		
		String uId = c.getString(0);
		String uPw = c.getString(1);
		String isAuto = c.getString(2);
		
		
		if(uId !=null && uPw != null){
			idField.setText(uId);
			idField.setEnabled(false);
			pwField.setText(uPw);
			pwField.setEnabled(false);
			if(isAuto!=null && isAuto.equals("true")) autoLoginChk.setChecked(true);
			else if(isAuto!=null && isAuto.equals("false")) autoLoginChk.setChecked(false);
		}
		
		
		
	}
}
